trigger ActiveEfectiveDateMark on Hospital__c (after update) {
  /*  List<Hospital__c> hpl=new List<Hospital__c>();
    for(Hospital__c hp:trigger.new){
        Date tmp = hp.effectivedate__c;
        if(Trigger.oldMap.get(hp.Id).effectivedate__c != hp.effectivedate__c ){
            hpl=[select id /*,EffectiveDateMark__c/ from hospital__c where id=:hp.id];
            if(hpl.size()>0){
                Hospital__c hospital=hpl[0];
                //hospital.EffectiveDateMark__c = 1;
                update hpl;
            }    
        }
    }*/
    //判断是否进入代码
    boolean b=false;
    for(hospital__c h:trigger.new){
        if(Trigger.oldMap.get(h.Id).effectivedate__c != h.effectivedate__c &&(h.effectivedate__c!=null) ){
            b=true;
        }
    }
    if(b){
    
    //搜索出所有医院
    list<id> hosid=new list<id>();
    for(hospital__c h:trigger.new){
        hosid.add(h.id);
    }
    //搜索出审批医院
    list<hospital_approve__c> hosApproList =[select id,ownerchangeto__c,hospital__c from hospital_approve__c where hospital__c in:hosid and approvaltype__c ='改变MSR'];
    Map<id,hospital_approve__c> hosappromap=new map<id,hospital_approve__c>();
    for(hospital_approve__c h:hosapprolist){
        hosappromap.put(h.hospital__c,h);
    }
    /*
    //搜索所有HCP
    list<HCP__c> hcplist=[select id,hospital__c from HCP__c where Hospital__c in:hosid and hospital__r.approvaltype__c ='改变MSR'];
    list<hcp_share>
    if(hcplist.size()>0){
        
    }
    */
    
    list<hospital__c> hosl=[select id,approvalstatus__c,Effectivedate__c from hospital__c where id in:hosid];
    //清空医院审批状态  用以清除工作流
    for(hospital__c h:hosl){
        h.approvalstatus__c='';
    }
    update hosl;
    //重新填入审批状态
    for(hospital__c h:hosl){
        h.approvalstatus__c='审批通过但未生效';
    }
    update hosl;
    //搜索出所有触发医院生效记录
    list<HospitalEffectivedateTrigger__c> hedtl=[select id from HospitalEffectivedateTrigger__c where hospital__c in:hosid];
    //删除所有触发医院生效记录  用以清除工作流
    delete hedtl;
    //重新填入触发医院生效记录
    list<HospitalEffectivedateTrigger__c> insertl=new list<HospitalEffectivedateTrigger__c>();
    list<Hospital__Share> insertshare=new list<Hospital__Share>();
    for(hospital__c h:trigger.new){
         if(hosappromap.size()>0){
           if(hosappromap.containskey(h.id)){
              hospital__Share sharehos= new hospital__Share();
              sharehos.ParentId = hosappromap.get(h.id).hospital__c;
              sharehos.UserOrGroupId = hosappromap.get(h.id).ownerchangeto__c;
              sharehos.AccessLevel = 'Edit';
              insertshare.add(sharehos);
            }
         }
         HospitalEffectivedateTrigger__c hedt=new HospitalEffectivedateTrigger__c(name=h.name,hospital__c=h.id,Effectivedate__c=h.effectivedate__c);
         insertl.add(hedt);
    }
    insert insertl;
    insert insertshare;
    }
    /*  List<Hospital__c> hpl=new List<Hospital__c>();
      List<HospitalEffectivedateTrigger__c> hedtl=new List<HospitalEffectivedateTrigger__c>();
    for(Hospital__c hp:trigger.new){
        Date tmp = hp.effectivedate__c;
          if(Trigger.oldMap.get(hp.Id).effectivedate__c != hp.effectivedate__c &&(hp.effectivedate__c!=null) ){
            hedtl=[select id from HospitalEffectivedateTrigger__c where hospital__c=:hp.id];
            hpl=[select id,approvalstatus__c,Effectivedate__c from hospital__c where id=:hp.id];
            if(hpl.size()>0){
                Hospital__c hospital=hpl[0];
                hospital.approvalstatus__c='';
                update hospital;
                hospital.approvalstatus__c='审批通过但未生效';
                update hospital;
              if(hedtl.size()>0){
              delete hedtl;
              }
              HospitalEffectivedateTrigger__c hedt=new HospitalEffectivedateTrigger__c(name=hp.name,hospital__c=hp.id,Effectivedate__c=hp.effectivedate__c);
              insert hedt; 
            }    
        }
    }
  */
    
}